import {createRouter, createWebHistory} from 'vue-router'
import {useCounterStore} from '@/stores/counter.js'

const router = createRouter({
    history: createWebHistory(import.meta.env.BASE_URL),
    linkActiveClass: 'active-router',
    routes: [
        {
            path: '/login',
            name: 'login',
            component: () => import('../views/login.vue')
        },
        // {
        //   path:'/404',
        //   name:'404',
        //   component:()=>import('../views/404.vue'),
        //   hidden:true
        // },
        // {
        //   path:`/:pathMatch(.*)`,
        //   redirect:'/404',
        //   hidden:true
        // },
        {
            path: '/',
            name: 'Layout',
            component: () => import('../views/LayoutView.vue'),
            redirect: '/home',
            children: [
                {
                    path: 'home',
                    name: 'home',
                    component: () => import('../components/home.vue')
                },
                {
                    path: '/classification',
                    name: 'classification',
                    redirect: '/classification/table',
                    component: () => import('../components/classification.vue'),
                    children: [
                        {
                            path: 'table',
                            name: 'classification_table',
                            component: () => import('../components/table/classification_table.vue')
                        },
                        {
                            path: 'form',
                            name: 'classification_form',
                            component: () => import('../components/form/classification_form.vue')
                        }
                    ]
                },
                {
                    path: '/inventory',
                    name: 'inventory',
                    redirect: '/inventory/table',
                    component: () => import('../components/inventory.vue'),
                    children: [
                        {
                            path: 'table',
                            name: 'inventory_table',
                            component: () => import('../components/table/inventory_table.vue')
                        },
                        {
                            path: 'form',
                            name: 'inventory_form',
                            component: () => import('../components/form/inventory_form.vue')
                        }
                    ]
                },
                {
                    path: '/enterStore',
                    name: 'enterStore',
                    redirect: '/enterStore/table',
                    component: () => import('../components/enterStore.vue'),
                    children: [
                        {
                            path: 'table',
                            name: 'enterStore_table',
                            component: () => import('../components/table/enterStore_table.vue')
                        },
                        {
                            path: 'form',
                            name: 'enterStore_form',
                            component: () => import('../components/form/enterStore_form.vue')
                        }
                    ]
                },
                {
                    path: '/earlyWarning',
                    name: 'earlyWarning',
                    redirect: '/earlyWarning/table',
                    component: () => import('../components/earlyWarning.vue'),
                    children: [
                        {
                            path: 'table',
                            name: 'earlyWarning_table',
                            component: () => import('../components/table/earlyWarning_table.vue')
                        },
                        {
                            path: 'form',
                            name: 'earlyWarning_form',
                            component: () => import('../components/form/earlyWarning_form.vue')
                        }
                    ]
                },
                {
                    path: '/leaveStore',
                    name: 'leaveStore',
                    redirect: '/leaveStore/table',
                    component: () => import('../components/leaveStore.vue'),
                    children: [
                        {
                            path: 'table',
                            name: 'leaveStore_table',
                            component: () => import('../components/table/leaveStore_table.vue')
                        },
                        {
                            path: 'form',
                            name: 'leaveStore_form',
                            component: () => import('../components/form/leaveStore_form.vue')
                        },
                        {
                            path: 'wastage_form',
                            name: 'wastage_form',
                            component: () => import('../components/form/wastage_form.vue')
                        }
                    ]
                },
                {
                  path:'/user',
                  name:'user',
                  redirect:'/user/table',
                  component:()=>import('../components/user.vue'),
                  children:[
                    {
                      path:'table',
                      name:'user_table',
                      component:()=>import('../components/table/user_table.vue')
                    },
                    {
                      path:'form',
                      name:'user_form',
                      component:()=>import('../components/form/user_form.vue')
                    }
                  ]
                },
                {
                    path:'/price',
                    name:'price',
                    redirect:'/price/table',
                    component:()=>import('../components/price.vue'),
                    children:[
                        {
                            path: 'table',
                            name:'price_table',
                            component:()=>import('../components/table/price_table.vue')
                        },
                        {
                            path: 'form',
                            name:'price_form',
                            component:()=>import('../components/form/price_form.vue')
                        }
                    ]
                }
            ]
        }
    ]
})
router.beforeEach((to, from, next) => {
    const tokenObj = useCounterStore().token
    if (to.path === "/login") {
        next()
    } else {
        if (tokenObj == null || tokenObj === "") {
            next('/login')
        } else {
            next()
        }
    }
})
export default router
